package com.qyhl.module_practice.volunteer.sign.search;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.module_practice.volunteer.sign.search.item.ItemPracticeSeacrhOrg;
import com.qyhl.module_practice.volunteer.sign.search.item.ItemPracticeSearchTitle;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeSignOrgBean;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 文明办搜索功能
 */
@Route(path = ARouterPathConstant.PRACTICE_TAG_SEARCH)
public class PracticeOrigationSearchActivity extends BaseActivity {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.search_txt)
    EditText searchTxt;

    private List<PracticeSignOrgBean> mDataList;

    private MultiItemTypeAdapter<SearchIndexBean> mAdapter;

    private List<SearchIndexBean> indexList = new ArrayList<>();

    private LinearLayoutManager linearLayoutManager;

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_tag_search;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mDataList = (List<PracticeSignOrgBean>) getIntent().getSerializableExtra("list");
        initData();
        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);
        mAdapter = new MultiItemTypeAdapter<>(this, indexList);
        mAdapter.addItemViewDelegate(new ItemPracticeSeacrhOrg(this));
        mAdapter.addItemViewDelegate(new ItemPracticeSearchTitle(this));
        recycleView.setAdapter(mAdapter);

    }

    @Override
    protected void setListener() {
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                SearchIndexBean bean = indexList.get(position);
                if (bean.getType() == 1) {
                    for (int i = 0; i < indexList.size(); i++) {
                        if (indexList.get(i).getType() == 1) {
                            if (i == position) {
                                indexList.get(i).getBean().setChecked(true);
                            } else {
                                indexList.get(i).getBean().setChecked(false);
                            }
                        }
                    }

                    for (int i = 0; i < mDataList.size(); i++) {
                        List<PracticeSignOrgBean.PracticeSignOrgList> beanList
                                = mDataList.get(i).getOrgList();
                        for (int j = 0; j < beanList.size(); j++) {
                            if (beanList.get(j).getId() == bean.getBean().getId()) {
                                beanList.get(j).setChecked(true);
                            } else {
                                beanList.get(j).setChecked(false);
                            }
                        }
                    }
                }

//                SearchIndexBean bean = indexList.get(position);
//                boolean isChecked = !bean.getBean().isChecked();
//                bean.getBean().setChecked(isChecked);
//                if (bean.getType() == 1) {
//                    for (int i = 0; i < mDataList.size(); i++) {
//                        List<PracticeSignOrgBean.PracticeSignOrgList> beanList
//                                = mDataList.get(i).getOrgList();
//                        for (int j = 0; j < beanList.size(); j++) {
//                            if (beanList.get(j).getId() == bean.getBean().getId()) {
//                                beanList.get(j).setChecked(isChecked);
//                            }
//                        }
//                    }
//                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (StringUtils.isEmpty(searchTxt.getText().toString())) {
                    showToast("请输入关键字！", 4);
                } else {
                    for (int i = 0; i < indexList.size(); i++) {
                        if (indexList.get(i).getName().contains(searchTxt.getText().toString())) {
//                            recycleView.scrollToPosition(i);
                            linearLayoutManager.scrollToPositionWithOffset(i, 0);
                            return false;
                        }
                    }
                    showToast("无相关内容！", 4);
                }
                return false;//返回true，保留软键盘。false，隐藏软键盘
            }
        });
    }


    private void initData() {
        if (mDataList != null && mDataList.size() > 0) {
            for (int i = 0; i < mDataList.size(); i++) {
                indexList.add(new SearchIndexBean(0, mDataList.get(i).getInsName(), null));
                List<PracticeSignOrgBean.PracticeSignOrgList> beanList
                        = mDataList.get(i).getOrgList();
                for (int j = 0; j < beanList.size(); j++) {
                    indexList.add(new SearchIndexBean(1, beanList.get(j).getName(), beanList.get(j)));
                }
            }
        }
    }

    @OnClick({R2.id.back_btn, R2.id.commit, R2.id.search_btn})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
            hideInput();
        } else if (id == R.id.commit) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("list", (Serializable) mDataList);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        } else if (id == R.id.search_btn) {
            if (StringUtils.isEmpty(searchTxt.getText().toString())) {
                showToast("请输入关键字！", 4);
            } else {
                for (int i = 0; i < indexList.size(); i++) {
                    if (indexList.get(i).getName().contains(searchTxt.getText().toString())) {
//                            recycleView.scrollToPosition(i);
                        linearLayoutManager.scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
                showToast("无相关内容！", 4);
            }
        }
    }

    public class SearchIndexBean implements Serializable {
        private int type;
        private String name;
        private PracticeSignOrgBean.PracticeSignOrgList bean;

        SearchIndexBean(int type, String name, PracticeSignOrgBean.PracticeSignOrgList bean) {
            this.type = type;
            this.name = name;
            this.bean = bean;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public PracticeSignOrgBean.PracticeSignOrgList getBean() {
            return bean;
        }

        public void setBean(PracticeSignOrgBean.PracticeSignOrgList bean) {
            this.bean = bean;
        }
    }
}
